package com.example.managemyself.ui.diary;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.managemyself.R;
import com.example.managemyself.data.model.DiaryEntry;
import com.google.android.material.card.MaterialCardView;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * 日记列表适配器 - 笔记本风格设计
 */
public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.DiaryViewHolder> {
    
    private List<DiaryEntry> diaryEntries;
    private OnDiaryItemClickListener onItemClickListener;
    private OnDiaryItemLongClickListener onItemLongClickListener;
    
    private SimpleDateFormat dateFormat;
    private SimpleDateFormat timeFormat;
    
    public interface OnDiaryItemClickListener {
        void onDiaryItemClick(DiaryEntry diaryEntry);
    }
    
    public interface OnDiaryItemLongClickListener {
        void onDiaryItemLongClick(DiaryEntry diaryEntry);
    }
    
    public DiaryAdapter(List<DiaryEntry> diaryEntries, 
                       OnDiaryItemClickListener onItemClickListener,
                       OnDiaryItemLongClickListener onItemLongClickListener) {
        this.diaryEntries = diaryEntries;
        this.onItemClickListener = onItemClickListener;
        this.onItemLongClickListener = onItemLongClickListener;
        
        this.dateFormat = new SimpleDateFormat("MM月dd日", Locale.getDefault());
        this.timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
    }
    
    @NonNull
    @Override
    public DiaryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_diary_entry, parent, false);
        return new DiaryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull DiaryViewHolder holder, int position) {
        DiaryEntry diaryEntry = diaryEntries.get(position);
        holder.bind(diaryEntry);
    }
    
    @Override
    public int getItemCount() {
        return diaryEntries.size();
    }
    
    class DiaryViewHolder extends RecyclerView.ViewHolder {
        
        private MaterialCardView cardDiary;
        private TextView tvDate;
        private TextView tvTime;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvMood;
        private TextView tvWeather;
        
        public DiaryViewHolder(@NonNull View itemView) {
            super(itemView);
            
            cardDiary = itemView.findViewById(R.id.card_diary);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvMood = itemView.findViewById(R.id.tv_mood);
            tvWeather = itemView.findViewById(R.id.tv_weather);
        }
        
        public void bind(DiaryEntry diaryEntry) {
            // 设置日期和时间
            if (diaryEntry.getDate() != null) {
                tvDate.setText(dateFormat.format(diaryEntry.getDate()));
                tvTime.setText(timeFormat.format(diaryEntry.getDate()));
            } else {
                tvDate.setText("--");
                tvTime.setText("--");
            }
            
            // 设置标题
            if (diaryEntry.getTitle() != null && !diaryEntry.getTitle().trim().isEmpty()) {
                tvTitle.setText(diaryEntry.getTitle());
                tvTitle.setVisibility(View.VISIBLE);
            } else {
                tvTitle.setVisibility(View.GONE);
            }
            
            // 设置内容预览（最多显示2行）
            if (diaryEntry.getContent() != null && !diaryEntry.getContent().trim().isEmpty()) {
                String content = diaryEntry.getContent().trim();
                if (content.length() > 100) {
                    content = content.substring(0, 100) + "...";
                }
                tvContent.setText(content);
                tvContent.setVisibility(View.VISIBLE);
            } else {
                tvContent.setText("无内容");
                tvContent.setVisibility(View.VISIBLE);
            }
            
            // 设置心情
            if (diaryEntry.getMood() != null && !diaryEntry.getMood().trim().isEmpty()) {
                tvMood.setText("心情: " + diaryEntry.getMood());
                tvMood.setVisibility(View.VISIBLE);
            } else {
                tvMood.setVisibility(View.GONE);
            }
            
            // 设置天气
            if (diaryEntry.getWeather() != null && !diaryEntry.getWeather().trim().isEmpty()) {
                tvWeather.setText("天气: " + diaryEntry.getWeather());
                tvWeather.setVisibility(View.VISIBLE);
            } else {
                tvWeather.setVisibility(View.GONE);
            }
            
            // 设置点击事件
            cardDiary.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onDiaryItemClick(diaryEntry);
                }
            });
            
            cardDiary.setOnLongClickListener(v -> {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onDiaryItemLongClick(diaryEntry);
                }
                return true;
            });
            
            // 设置卡片样式（笔记本风格）
            cardDiary.setCardElevation(4f);
            cardDiary.setRadius(12f);
        }
    }
}
